//
//  WPDrawCashViewController.m
//  ZhongChouPingTai
//
//  Created by WeiPan on 16/4/1.
//  Copyright © 2016年 WPJoe. All rights reserved.
//

#import "WPDrawCashViewController.h"
#import "WPGetTestCode.h"
#import "WPDrawCashViewModel.h"
#import "WPUserInfoViewModel.h"

@interface WPDrawCashViewController ()
@property (nonatomic, strong) UILabel * firstTopLabel;/**< 提示文本1 */
@property (nonatomic, strong) UITextField * moneyCountText;/**< 可提现金额 */
@property (nonatomic, strong) UITextField * topupTextField;/**< 提现金额 */
@property (nonatomic, strong) UITextField * accountTextField;/**< 支付宝账户 */
@property (nonatomic, strong) UITextField * accountNameTextField;/**< 支付宝账号名 */
@property (nonatomic, strong) UILabel * balanceLabel;/**< 余额文本 */
@property (nonatomic, strong) UIView * codeBackView;/**< 验证码背景图 */
@property (nonatomic, strong) UITextField * codeTextField;/**< 验证码 */
@property (nonatomic, strong) UITextField * pwdTextField;/**< 密码输入框 */
@property (nonatomic, strong) UILabel * introLabel;/**< 提现说明 */
@property (nonatomic, strong) UIButton * drawCashBtn;/**< 提现按钮 */
@property (nonatomic, strong) UIButton * getCodeBtn;/**< 获取验证码按钮 */
@property (nonatomic, strong) NSTimer * timer;/**< 计时器 */
@property (nonatomic, assign) NSInteger timeCount;/**< 时间记录 */

- (void)initializeUserInterface; /**< 初始化用户界面 */
@end

@implementation WPDrawCashViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initializeUserInterface];
}
#pragma mark - init
- (void)initializeUserInterface {
    [self.view addSubview:self.firstTopLabel];
    [self.view addSubview:self.accountNameTextField];
    [self.view addSubview:self.accountTextField];
    [self.view addSubview:self.balanceLabel];
    [self.view addSubview:self.topupTextField];
    [self.view addSubview:self.codeBackView];
    [self.view addSubview:self.pwdTextField];
    [self.view addSubview:self.drawCashBtn];
    [self.view addSubview:self.introLabel];
}
#pragma mark - responds events
- (void)textFieldDidChange:(UITextField *)textField {
    if (textField == _accountNameTextField && textField.text.length > 40) {
        textField.text = [textField.text substringToIndex:40];
    }
    if (textField == _accountTextField && textField.text.length > 20) {
        textField.text = [textField.text substringToIndex:20];
    }
    if (textField == _topupTextField && [textField.text floatValue] > [_balance floatValue]) {
        [self initializeAlertControllerWithMessageAndDismiss:@"提现金额大于当前余额"];
        textField.text = [textField.text substringToIndex:textField.text.length - 1];
    }
    if (textField == _codeTextField && textField.text.length > 6) {
        textField.text = [textField.text substringToIndex:6];
    }
    if (textField == _pwdTextField && textField.text.length > 14) {
        textField.text = [textField.text substringToIndex:14];
    }
}
- (void)respondToGetCodeBtn:(UIButton *)sender {
    [self.view endEditing:YES];
    [sender setTitle:@"60s再次获取" forState:UIControlStateNormal];
    sender.userInteractionEnabled = NO;
    [self.timer setFireDate:[NSDate distantPast]];
    _timeCount = 60;
    [WPGetTestCode getTestCodeWithPhone:[UserModel defaultUser].phoneNumber andSuccessBlock:^(NSString *result) {
    } andFailureBlock:^(NSString *fail) {
    }];
}
- (void)respondsToTimer {
    self.timeCount--;
    NSString * timeStr = [NSString stringWithFormat:@"%lds再次获取", (long)_timeCount];
    [_getCodeBtn setTitle:timeStr forState:UIControlStateNormal];
    if (_timeCount == 0) {
        [self.timer setFireDate:[NSDate distantFuture]];
        [_getCodeBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
        _getCodeBtn.userInteractionEnabled = YES;
    }
}
- (void)respondsToDrawCashBtn {
    if (_accountNameTextField.text.length == 0) {
        [self initializeAlertControllerWithMessageAndDismiss:@"请输入提现到账账户姓名"];
        return;
    }
    if (_accountTextField.text.length == 0) {
        [self initializeAlertControllerWithMessageAndDismiss:@"请输入提现账户"];
        return;
    }
    if (_topupTextField.text.length == 0) {
        [self initializeAlertControllerWithMessageAndDismiss:@"请输入提现金额"];
        return;
    }
    if (_codeTextField.text.length == 0) {
        [self initializeAlertControllerWithMessageAndDismiss:@"请输入验证码"];
        return;
    }
    if (_pwdTextField.text.length < 6) {
        [self initializeAlertControllerWithMessageAndDismiss:@"请输入至少6位数提现密码"];
        return;
    }
    MBProgressHUD * hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.removeFromSuperViewOnHide = YES;
    hud.color = [UIColor lightGrayColor];
    hud.labelText = @"请稍候";
    hud.detailsLabelText = @"提交申请中...";
    __weak typeof(self)weakself = self;
    [WPDrawCashViewModel drawCashWithMoneyCount:_topupTextField.text andWithAccountName:_accountNameTextField.text andWithAccount:_accountTextField.text andWithCode:_codeTextField.text andPayPwd:_pwdTextField.text andSuccessBlock:^(NSString * succ) {
        hud.labelText = succ;
        hud.detailsLabelText = @"请耐心等待审核";
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [hud hide:YES];
            [weakself.navigationController popViewControllerAnimated:YES];
        });
    } andFailureBlock:^(NSString * fail) {
        hud.labelText = fail;
        hud.detailsLabelText = nil;
        [hud hide:YES afterDelay:2.0];
    }];

}
#pragma mark - getter
- (UILabel *)firstTopLabel {
    if (!_firstTopLabel) {
        _firstTopLabel = ({
            UILabel * lab = [[UILabel alloc]initWithFrame:CGRectMake(10*DHFlexibleHorizontalMutiplier(), navBar_Fheight + 10*DHFlexibleHorizontalMutiplier(), 300, 25*DHFlexibleHorizontalMutiplier())];
            lab.font = [UIFont systemFontOfSize:10*DHFlexibleHorizontalMutiplier()];
            lab.textColor = COLOR_RGB(181, 181, 181, 1);
            lab.text = @"目前仅支持支付宝账户提现";
            lab;
        });
    }
    return _firstTopLabel;
}
- (UITextField *)accountNameTextField {
    if (!_accountNameTextField) {
        _accountNameTextField = ({
            UITextField * text = [[UITextField alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(self.firstTopLabel.frame), SCREEN_SIZE.width, 35*DHFlexibleHorizontalMutiplier())];
            UIView * leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 100*DHFlexibleHorizontalMutiplier(), 35*DHFlexibleHorizontalMutiplier())];
            UILabel * lab = [[UILabel alloc]initWithFrame:CGRectMake(10*DHFlexibleHorizontalMutiplier(), 0, 90*DHFlexibleHorizontalMutiplier(), 35*DHFlexibleHorizontalMutiplier())];
            [leftView addSubview:lab];
            text.leftViewMode = UITextFieldViewModeAlways;
            text.leftView = leftView;
            lab.text = @"姓名";
            lab.font = [UIFont systemFontOfSize:14*DHFlexibleHorizontalMutiplier()];
            lab.textColor = [UIColor colorWithHexString:@"#616161"];
            text.backgroundColor = [UIColor whiteColor];
            text.font = [UIFont systemFontOfSize:12*DHFlexibleHorizontalMutiplier()];
            UIView * lineUp = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_SIZE.width, 0.5)];
            lineUp.backgroundColor = [UIColor colorWithHexString:@"#eeeeee"];
            [text addSubview:lineUp];
            UIView * lineDown = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetHeight(text.bounds)-0.5, SCREEN_SIZE.width, 0.5)];
            lineDown.backgroundColor = [UIColor colorWithHexString:@"#eeeeee"];
            [text addSubview:lineDown];
            [text addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
            text.placeholder = @"提现到账账户持有人姓名";
            text;
        });
    }
    return _accountNameTextField;
}
- (UITextField *)accountTextField {
    if (!_accountTextField) {
        _accountTextField = ({
            UITextField * text = [[UITextField alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(self.accountNameTextField.frame), SCREEN_SIZE.width, 35*DHFlexibleHorizontalMutiplier())];
            UIView * leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 100*DHFlexibleHorizontalMutiplier(), 35*DHFlexibleHorizontalMutiplier())];
            UILabel * lab = [[UILabel alloc]initWithFrame:CGRectMake(10*DHFlexibleHorizontalMutiplier(), 0, 90*DHFlexibleHorizontalMutiplier(), 35*DHFlexibleHorizontalMutiplier())];
            lab.font = [UIFont systemFontOfSize:14*DHFlexibleHorizontalMutiplier()];
            [leftView addSubview:lab];
            text.leftViewMode = UITextFieldViewModeAlways;
            text.leftView = leftView;
            lab.text = @"账户";
            lab.textColor = [UIColor colorWithHexString:@"#616161"];
            text.backgroundColor = [UIColor whiteColor];
            text.font = [UIFont systemFontOfSize:12*DHFlexibleHorizontalMutiplier()];
            UIView * lineDown = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetHeight(text.bounds)-1, SCREEN_SIZE.width, 1)];
            lineDown.backgroundColor = [UIColor colorWithHexString:@"#eeeeee"];
            [text addSubview:lineDown];
            [text addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
            text.placeholder = @"提现到账账户账号";
            text;
        });
    }
    return _accountTextField;
}
- (UILabel *)balanceLabel {
    if (!_balanceLabel) {
        _balanceLabel = ({
            UILabel * lab = [[UILabel alloc]initWithFrame:CGRectMake(10*DHFlexibleHorizontalMutiplier(), CGRectGetMaxY(self.accountTextField.frame), 300, 25*DHFlexibleHorizontalMutiplier())];
            lab.font = [UIFont systemFontOfSize:10*DHFlexibleHorizontalMutiplier()];
            lab.textColor = COLOR_RGB(181, 181, 181, 1);
            lab.text = [NSString stringWithFormat:@"可提现金额%@元", _balance];
            lab;
        });
    }
    return _balanceLabel;
}
- (UITextField *)topupTextField {
    if (!_topupTextField) {
        _topupTextField = ({
            UITextField * text = [[UITextField alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(self.balanceLabel.frame), SCREEN_SIZE.width, 35*DHFlexibleHorizontalMutiplier())];
            UIView * leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 100*DHFlexibleHorizontalMutiplier(), 35*DHFlexibleHorizontalMutiplier())];
            UILabel * lab = [[UILabel alloc]initWithFrame:CGRectMake(10*DHFlexibleHorizontalMutiplier(), 0, 90, 35*DHFlexibleHorizontalMutiplier())];
            [leftView addSubview:lab];
            text.leftViewMode = UITextFieldViewModeAlways;
            text.leftView = leftView;
            lab.text = @"金额(元)";
            lab.font = [UIFont systemFontOfSize:14*DHFlexibleHorizontalMutiplier()];
            lab.textColor = [UIColor colorWithHexString:@"#616161"];
            text.backgroundColor = [UIColor whiteColor];
            text.font = [UIFont systemFontOfSize:12*DHFlexibleHorizontalMutiplier()];
            UIView * lineDown = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetHeight(text.bounds)-1, SCREEN_SIZE.width, 1)];
            lineDown.backgroundColor = [UIColor colorWithHexString:@"#eeeeee"];
            [text addSubview:lineDown];
            text.placeholder = @"请输入提现金额";
            text.keyboardType = UIKeyboardTypeDecimalPad;
            text.clearButtonMode = UITextFieldViewModeWhileEditing;
            [text addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
            text;
        });
    }
    return _topupTextField;
}
- (UIView *)codeBackView {
    if (!_codeBackView) {
        _codeBackView = ({
            UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(self.topupTextField.frame), SCREEN_SIZE.width, 35*DHFlexibleHorizontalMutiplier())];
            view.backgroundColor = [UIColor whiteColor];
            UIView * lineDown = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetHeight(view.bounds)-0.5, SCREEN_SIZE.width, 0.5)];
            lineDown.backgroundColor = [UIColor colorWithHexString:@"#eeeeee"];
            [view addSubview:lineDown];
            [view addSubview:self.codeTextField];
            [view addSubview:self.getCodeBtn];
            view;
        });
    }
    return _codeBackView;
}
- (UITextField *)codeTextField {
    if (!_codeTextField) {
        _codeTextField = ({
            UITextField * text = [[UITextField alloc]initWithFrame:CGRectMake(10*DHFlexibleHorizontalMutiplier(), 0, 220*DHFlexibleHorizontalMutiplier(), 35*DHFlexibleHorizontalMutiplier())];
            text.leftViewMode = UITextFieldViewModeAlways;
            UIView * leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 90*DHFlexibleHorizontalMutiplier(), 35*DHFlexibleHorizontalMutiplier())];
            UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 60*DHFlexibleHorizontalMutiplier(), 35*DHFlexibleHorizontalMutiplier())];
            label.text = @"验证码";
            label.font = [UIFont systemFontOfSize:14*DHFlexibleHorizontalMutiplier()];
            label.textColor = [UIColor colorWithHexString:@"#616161"];
            [leftView addSubview:label];
            text.leftView = leftView;
            text.font = [UIFont systemFontOfSize:13*DHFlexibleHorizontalMutiplier()];
            text.clearButtonMode = UITextFieldViewModeWhileEditing;
            text.keyboardType = UIKeyboardTypeNumberPad;
            [text addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
            text.placeholder = @"请输入短信验证码";
            text;
        });
    }
    return _codeTextField;
}
- (UIButton *)getCodeBtn {
    if (!_getCodeBtn) {
        _getCodeBtn = ({
            UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
            btn.frame = CGRectMake(240*DHFlexibleHorizontalMutiplier(), 5*DHFlexibleHorizontalMutiplier(), 70*DHFlexibleHorizontalMutiplier(), 25*DHFlexibleHorizontalMutiplier());
            btn.backgroundColor = COLOR_RGB(225, 225, 225, 1);
            btn.titleLabel.font = [UIFont systemFontOfSize:12*DHFlexibleHorizontalMutiplier()];
            [btn setTitle:@"获取验证码" forState:UIControlStateNormal];
            [btn addTarget:self action:@selector(respondToGetCodeBtn:) forControlEvents:UIControlEventTouchUpInside];
            btn;
        });
    }
    return _getCodeBtn;
}
- (NSTimer *)timer {
    if (!_timer) {
        _timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(respondsToTimer) userInfo:nil repeats:YES];
    }
    return _timer;
}
- (UITextField *)pwdTextField {
    if (!_pwdTextField) {
        _pwdTextField = ({
            UITextField * text = [[UITextField alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(self.codeBackView.frame), SCREEN_SIZE.width, 35*DHFlexibleHorizontalMutiplier())];
            UIView * leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 100*DHFlexibleHorizontalMutiplier(), 35*DHFlexibleHorizontalMutiplier())];
            UILabel * lab = [[UILabel alloc]initWithFrame:CGRectMake(10*DHFlexibleHorizontalMutiplier(), 0, 90*DHFlexibleHorizontalMutiplier(), 35*DHFlexibleHorizontalMutiplier())];
            [leftView addSubview:lab];
            text.leftViewMode = UITextFieldViewModeAlways;
            text.leftView = leftView;
            lab.text = @"提现密码";
            lab.font = [UIFont systemFontOfSize:14*DHFlexibleHorizontalMutiplier()];
            lab.textColor = [UIColor colorWithHexString:@"#616161"];
            text.backgroundColor = [UIColor whiteColor];
            text.font = [UIFont systemFontOfSize:12*DHFlexibleHorizontalMutiplier()];
            UIView * lineDown = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetHeight(text.bounds)-1, SCREEN_SIZE.width, 1)];
            lineDown.backgroundColor = [UIColor colorWithHexString:@"#eeeeee"];
            [text addSubview:lineDown];
            [text addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
            text.placeholder = @"请输入提现密码";
            text.secureTextEntry = YES;
            text.clearButtonMode = UITextFieldViewModeWhileEditing;
            text;
        });
    }
    return _pwdTextField;
}
- (UILabel *)introLabel {
    if (!_introLabel) {
        _introLabel = ({
            UILabel * lab = [[UILabel alloc]initWithFrame:CGRectMake(10*DHFlexibleHorizontalMutiplier(), CGRectGetMaxY(self.pwdTextField.frame), 300*DHFlexibleHorizontalMutiplier(), 25*DHFlexibleHorizontalMutiplier())];
            lab.textColor = COLOR_RGB(181, 181, 181, 1);
            lab.font = [UIFont systemFontOfSize:10*DHFlexibleHorizontalMutiplier()];
            lab.text = @"申请提交后我们将在两个工作日内处理";
            lab;
        });
    }
    return _introLabel;
}
- (UIButton *)drawCashBtn {
    if (!_drawCashBtn) {
        _drawCashBtn = ({
            UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
            btn.frame = CGRectMake(10*DHFlexibleHorizontalMutiplier(), CGRectGetMaxY(self.pwdTextField.frame) + 40*DHFlexibleHorizontalMutiplier(), 300*DHFlexibleHorizontalMutiplier(), 30*DHFlexibleHorizontalMutiplier());
            btn.backgroundColor = COLOR_RGB(234, 47, 47, 1);
            btn.layer.cornerRadius = 15*DHFlexibleHorizontalMutiplier();
            btn.titleLabel.font = [UIFont systemFontOfSize:13*DHFlexibleHorizontalMutiplier()];
            [btn setTitle:@"确定提现" forState:UIControlStateNormal];
            [btn addTarget:self action:@selector(respondsToDrawCashBtn) forControlEvents:UIControlEventTouchUpInside];
            btn;
        });
    }
    return _drawCashBtn;
}

@end
